<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Controllers;

use NRG\Framework\Exceptions\AuthException as AuthException;

/**
 * Error controller
 *
 * @package NRG\Framework\Controllers
 */
class Error extends BaseController
{
    /**
     * Exception object
     *
     * @var \Exception
     */
    protected $_exception;

    /**
     * Constructor
     *
     * @param \Exception $exception
     */
    public function __construct(Exception $exception)
    {
	    $this->_exception = $exception;

        if($exception instanceof AuthException)
            header("HTTP/1.0 401 Unauthorized");
        else
            header("HTTP/1.1 500 Internal Server Error");

        echo json_encode(array(	"error" => $this->_exception->getMessage(),
                    "status" => $exception->getType(),
                    "rfc" => "http://www.ietf.org/rfc/rfc2616.txt"));
        exit();
    }
}
